<%
remote_controller = active_scaffold_controller_for(column.association.klass)
select_options = options_for_select(options_for_association(column.association)) unless column.through_association?

show_add_new = (column.plural_association? or (column.singular_association? and not associated.empty?))
show_add_existing = (select_options and not select_options.empty?)
show_add_new = show_add_existing = false if column.through_association?
show_add_new = false unless @record.class.authorized_for?(:action => :create)

edit_associated_url = url_for(:action => 'edit_associated', :id => parent_record.id, :association => column.name, :associated_id => '--ID--', :_method => :get, :escape => false)
add_new_url = url_for(:action => 'edit_associated', :id => parent_record.id, :association => column.name, :_method => :get);

return unless show_add_new or show_add_existing

-%>
<div class="footer-wrapper">
  <div class="footer">
    <% if show_add_new -%>
    <% add_label = column.plural_association? ? as_('Create Another') : as_('Replace With New') -%>
    <%= button_to_function add_label, "new Ajax.Request(#{add_new_url.to_json}, {asynchronous: true, evalScripts: true, onFailure: function(){ActiveScaffold.report_500_response(#{active_scaffold_id})}})" %>
    <% end -%>

    <% if show_add_existing -%>
      |
      <% if remote_controller and remote_controller.respond_to? :uses_record_select? and remote_controller.uses_record_select? -%>
        <%= link_to_record_select as_('Add From Existing'), remote_controller.controller_path, :onselect => "new Ajax.Request(#{edit_associated_url.to_json}.sub('--ID--', id), {asynchronous: true, evalScripts: true, onFailure: function(){ActiveScaffold.report_500_response(#{active_scaffold_id})}}); container.remove();" -%>
      <% else -%>
        <%= select_tag 'associated_id', '<option value="">' + as_('- select -') + '</option>' + select_options %>
        <%= button_to_function as_('Add From Existing'), "new Ajax.Request(#{edit_associated_url.to_json}.sub('--ID--', Element.previous(this).value), {asynchronous: true, evalScripts: true, onFailure: function(){ActiveScaffold.report_500_response(#{active_scaffold_id})}})" %>
      <% end -%>
    <% end -%>
  </div>
</div>

<script type="text/javascript">
//Rico.Corner.round($$('#<%= sub_form_id(:association => column.name) %> .footer-wrapper').first(), {color: 'fromElement', bgColor: 'fromParent', compact: false});
</script>
